package com.itcat.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcat.api.mysql.UmsAdminRoleApi;
import com.itcat.api.mysql.UmsAdminRoleRelationApi;
import com.itcat.api.mysql.UmsMenuApi;
import com.itcat.common.CommonPage;
import com.itcat.interceptor.UserHolder;
import com.itcat.mysql.pojo.UmsMenu;
import com.itcat.mysql.pojo.UmsResource;
import com.itcat.mysql.pojo.UmsRole;
import com.itcat.service.UmsRoleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/4/2 12:42]
 */
@Service
public class UmsRoleServiceImpl implements UmsRoleService {

    @DubboReference
    private UmsAdminRoleApi umsAdminRoleApi;

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @DubboReference
    private UmsAdminRoleRelationApi umsAdminRoleRelationApi;

    @Autowired
    private AdminCacheServiceImpl service;

    /***
     * 获取所有角色
     * @author Cr Mr
     * @date 2022/4/3 0:41
     */
    @Override
    public List<UmsRole> listAll() {
        return umsAdminRoleApi.listAll();
    }

    /***
     * 根据角色名称分页获取角色列表
     * @author Cr Mr
     * @date 2022/4/3 0:41
     */
    @Override
    public CommonPage<UmsRole> getPage(String name, Integer pageNum, Integer pageSize) {
        Page<UmsRole> selectPage = umsAdminRoleApi.selectPage(name, pageNum, pageSize);
        CommonPage<UmsRole> page = CommonPage.restPage(selectPage);
        return page;
    }

    /***
     * 获取角色相关菜单
     * @author Cr Mr
     * @date 2022/4/3 8:50
     */
    @Override
    public List<UmsMenu> listMenu(Long roleId) {
        List<Long> roles = new ArrayList<>();
        roles.add(roleId);
        return umsMenuApi.getMenuList(roles);
    }

    /***
     * 添加角色
     * @author Cr Mr
     * @date 2022/4/3 10:57
     */
    @Override
    public void create(UmsRole umsRole) {
        umsAdminRoleApi.create(umsRole);
    }

    /***
     * 给角色分配菜单
     * @author Cr Mr
     * @date 2022/4/3 11:03
     */
    @Override
    public void allocMenu(Long roleId, List<Long> menuIds) {
        if (!CollUtil.isEmpty(menuIds)) {
            umsAdminRoleApi.allocMenu(roleId, menuIds);
        }
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 给角色分配资源
     * @author Cr Mr
     * @date 2022/4/3 11:14
     */
    @Override
    public void allocResource(Long roleId, List<Long> resourceIds) {
        if (!CollUtil.isEmpty(resourceIds)) {
            umsAdminRoleRelationApi.allocResource(roleId, resourceIds);
        }
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 批量删除角色
     * @author Cr Mr
     * @date 2022/4/3 11:26
     */
    @Override
    public void delete(List<Long> ids) {
        if (!CollUtil.isEmpty(ids)) {
            umsAdminRoleApi.delete(ids);
        }
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 获取角色相关资源
     * @author Cr Mr
     * @date 2022/4/3 13:04
     */
    @Override
    public List<UmsResource> listResource(Long roleId) {
        return umsAdminRoleRelationApi.listResource(roleId);
    }

    /***
     * 修改角色
     * @author Cr Mr
     * @date 2022/4/3 13:13
     */
    @Override
    public void update(Long id, UmsRole umsRole) {
        umsAdminRoleApi.update(id, umsRole);
        service.removeAdmin(UserHolder.get());
    }

    /***
     * 修改角色状态
     * @author Cr Mr
     * @date 2022/4/3 13:13
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        umsAdminRoleApi.updateStatus(id, status);
        service.removeAdmin(UserHolder.get());
    }
}
